﻿using static ch07.binary.BlockType;

namespace ch07.binary.wasm_reader
{
    public partial struct WasmReader
    {
        private BlockArgs ReadBlockArgs()
        {
            var args = new BlockArgs();
            byte end;
            args.BT = ReadBlockType();
            (args.Instrs, end) = readInstructions();
            if (end != OpCodes.End)
            {
                Panic.Run("invalid block end: {0:D}", end);
            }

            return args;
        }

        // entity types
        private BlockType ReadBlockType()
        {
            var bt = ReadVarS32();
            if (bt >= 0) return (BlockType) bt;
            switch ((BlockType) bt)
            {
                case BlockTypeI32:
                case BlockTypeI64:
                case BlockTypeF32:
                case BlockTypeF64:
                case BlockTypeEmpty:
                    break;
                default:
                    Panic.Run("malformed block type: {0:D}", bt);
                    break;
            }

            return (BlockType) bt;
        }
    }
}